% ************************************************************************ 
%     Module name   : Lynx Simulation - World Init function                                                   
%                                                                          
%     Description   : This will initialise the World.                                                    
%                                                                          
%     $Header: /Lynx/SimulationFramework/RSP/IFS/IFSRun.m 2     15/10/08 16:08 Lwabeke $ 
%                                                                          
%    $Author: Lwabeke $	            
%                                                                         
%    $Revision: 2 $                
%                                                                         
% ************************************************************************
%     $History: IFSRun.m $ 
% 
%
% ************************************************************************
function [worldState] = World_Init(time, param)

worldState = [];

worldState.wind = 5 *[ 0.707 0.707];

for numTargets = 1:param.numTargets;
    
    Target.id = numTargets;
    Target.type = 1; % Some target type
    Target.initTime = 0;
    Target.initPos = rand(1,2) .* [diff(param.world.area(1:2)), diff(param.world.area(3:4))] + param.world.area(1:2:3);
    Target.endTime = param.endTime + 1;
    Target.endPos = rand(1,2) .* [diff(param.world.area(1:2)), diff(param.world.area(3:4))] + param.world.area(1:2:3);
    Target.currentPos = Target.initPos;
    Target.currentVel = [0 0]; %(Target.endPos - Target.initPos) / (Target.endTime - Target.initTime);
    Target.currentProp = 1.42;
    Target.currentHeading = 0.5*pi;
    Target.currentTurnRate = 0;
    Target.sailfactor = 0.1;
    Target.found = 0;
    
    if isfield(worldState, 'Target')
        worldState.Target(end+1) = Target;
    else
        worldState.Target(1) = Target;
    end
end

% Setup clutter spikes
if ~isfield(param.world, 'clutterSpikes')
    param.world.clutterSpikes = 0;
end

init_times = rand(1,param.world.clutterSpikes)*param.endTime;
init_times = sort(init_times);

for cntr=1:param.world.clutterSpikes
    Target.id = numTargets+cntr;         
    Target.type = 0; % Use type 0 for clutter spikes
    Target.initTime = init_times(cntr);
    Target.initPos = (rand(1,2) - 0.5) .* 30000 ; %* [ 1 1.5 ; 0 1];
    Target.endTime = Target.initTime + rand(1) * 5;
    Target.endPos = Target.initPos + randn(1,2) .* 10;
    Target.currentPos = Target.initPos;
    Target.currentVel = randn(1,2) .* 4; %(Target.endPos - Target.initPos) / (Target.endTime - Target.initTime);
    Target.currentProp = 0;
    Target.currentHeading = 0;
    Target.currentTurnRate = 0;
    Target.sailfactor = 0;
    Target.found = 0;
    
%    worldState.Target(end+1) = Target;
    worldState.Target(numTargets+cntr) = Target;
end

worldState.currentTime = time;

worldState.activeTargets = 0;

worldState.numTargets = numTargets;
worldState.oldestActive = worldState.numTargets+1;

